//
//  MHSettingCircleJoinTypeViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/20.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSettingCircleJoinTypeViewModel.h"
#import "MHSetPriceItemViewModel.h"

@implementation MHSettingCircleJoinTypeViewModel



- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.circleInfo = params[MHViewModelUtilKey];
        
    }
    return self;
}

- (void)initialize{
    [super initialize];
    self.title = @"成员加入方式";
    
    
    if ([self.circleInfo currentUserIsOwner]) {
         self.rightTitle = @"保存";
    }
    MHCommonGroupViewModel *group0 = [MHCommonGroupViewModel groupViewModel];
    group0.footer = @"付费加入申请72小时未审批将失效，费用自动退回";
    CGFloat limitWidth = MH_SCREEN_WIDTH - 2 * 20;
    CGFloat footerHeight0 = [group0.footer mh_sizeWithFont:MHRegularFont_14 limitWidth:limitWidth].height + 5 *2;
    group0.footerHeight = footerHeight0;
    MHCommonGroupViewModel *group1 = [MHCommonGroupViewModel groupViewModel];
    MHCommonGroupViewModel *group2 = [MHCommonGroupViewModel groupViewModel];
    MHCommonGroupViewModel *group3 = [MHCommonGroupViewModel groupViewModel];
    MHCommonSwitchItemViewModel *needAuth = [MHCommonSwitchItemViewModel itemViewModelWithTitle:@"成员加入需要审批"];
    needAuth.off = !self.circleInfo.isNeedAudit;
    needAuth.offBlock = ^(BOOL isAuth) {
        self.circleInfo.isNeedAudit = !isAuth;
    };
    
    NSString *str = [NSString stringWithFormat:@"固定时长（￥%ld元/年)\n有效期：用户的加入日期往后计算1年",(long)self.circleInfo.joinPrice];
    MHCommonItemViewModel *duration = [MHCommonItemViewModel itemViewModelWithTitle:str];
    duration.leftDetialTile = @"有效期：用户的加入日期往后计算1年";
    duration.subtitle = @"";
    duration.rowHeight = 70;  
    MHSetPriceItemViewModel *price = [[MHSetPriceItemViewModel alloc] init];
    price.rowHeight = 168;
    price.price = [NSString stringWithFormat:@"%@",[@(self.circleInfo.joinPrice ?: 0) stringValue]];

    

    
    MHCommonArrowItemViewModel *discount = [MHCommonArrowItemViewModel itemViewModelWithTitle:[NSString stringWithFormat:@"续费价格（￥%ld/年）",(long)self.circleInfo.goOnPrice]];
    CGFloat discountValue = (CGFloat)self.circleInfo.goOnPrice/(CGFloat)self.circleInfo.joinPrice   *10;
    discount.subtitle =  discountValue == 10 ? @"无折扣": [NSString stringWithFormat:@"%.1f折",discountValue];
    group0.itemViewModels = @[needAuth];
    group1.itemViewModels = @[duration];
    group2.itemViewModels = @[discount];
    group3.itemViewModels = @[price];
    
    if(self.circleInfo.payType == MHCirclPayTypeFree)
    {
        self.dataSource = @[group0];
    }else
    {
        self.dataSource = @[group1,group3,group2];
    }
    
    
    
    @weakify(self);
    self.updateInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        return [self.services.client updateCircleSettingWithInfo:self.circleInfo];
    }];
    
}


@end
